package org.zhq.client;


import org.zhq.Foo;
import org.zhq.box.FileSendPacket;
import org.zhq.client.bean.ServerInfo;
import org.zhq.core.Connector;
import org.zhq.core.IoContext;
import org.zhq.core.ScheduleTask;
import org.zhq.core.SchedulerImpl;
import org.zhq.core.schedule.IdleTimeoutScheduleTask;
import org.zhq.handle.ConnectorCloseChain;
import org.zhq.handle.ConnectorHandler;
import org.zhq.impl.IoSelectorProvider;
import org.zhq.util.CloseUtils;

import java.io.*;
import java.util.concurrent.TimeUnit;

/**
 * @author zhengquan
 * @date 2018/12/23
 */
public class Client {

    public static void main(String[] args) throws IOException {
        File cachePath = Foo.getCacheDir("client");
        IoContext.setup()
                .ioProvider(new IoSelectorProvider())
                .scheduler(new SchedulerImpl(1))
                .start();
        //搜索10s服务器信息
        ServerInfo info = UDPSearcher.searchServer(10000);
        System.out.println("Server:" + info);

        if (info != null) {
            TCPClient tcpClient = null;
            try {
                tcpClient = TCPClient.startWith(info, cachePath);
                if (tcpClient == null) {
                    return;
                }
                tcpClient.getCloseChain().appendLast(new ConnectorCloseChain() {
                    @Override
                    protected boolean consume(ConnectorHandler handler, Connector connector) {
                        CloseUtils.close(System.in);
                        return true;
                    }
                });


                ScheduleTask scheduleTask = new IdleTimeoutScheduleTask(10, TimeUnit.SECONDS,tcpClient);
                tcpClient.schedule(scheduleTask);

                write(tcpClient);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("服务器异常");
            } finally {
                tcpClient.exit();
            }
        }
        IoContext.close();
    }

    private static void write(TCPClient tcpClient) throws IOException {
        //构建键盘输入流
        InputStream in = System.in;
        BufferedReader socketInput = new BufferedReader(new InputStreamReader(in));

        do {
            //键盘读取一行
            String msg = socketInput.readLine();
            if(msg==null || Foo.COMMAND_EXIT.equalsIgnoreCase(msg)){
                break;
            }
            if(msg.length()==0){
                continue;
            }
            //发送给服务器
            //文件格式 --f url
            if (msg.startsWith("--f")) {
                String[] array = msg.split(" ");
                if (array.length >= 2) {
                    String url = array[1];
                    File file = new File(url);
                    if(file.exists() && file.isFile()){
                        FileSendPacket packet = new FileSendPacket(file);
                        tcpClient.send(packet);
                        continue;
                    }
                }
            }
            //文字格式
            tcpClient.send(msg);
        } while (true);
    }
}
